﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace WorkflowDesigner.UI
{
    internal class ShapeStart : ShapeBase
    {
        private Point _point1;
        private Point _point2;
        private Point _point3;
        private Point _point4;

        private Point Hand_1
        {
            get
            {
                _point1.X = X + Width / 2;
                _point1.Y = Y;
                return _point1;
            }
        }

        private Point Hand_2
        {
            get
            {
                _point2.X = X + Width;
                _point2.Y = Y + Height / 2;
                return _point2;
            }
        }

        private Point Hand_3
        {
            get
            {
                _point3.X = X + Width / 2;
                _point3.Y = Y + Height;
                return _point3;
            }
        }

        private Point Hand_4
        {
            get
            {
                _point4.X = X;
                _point4.Y = Y + Height / 2;
                return _point4;
            }
        }

        public ShapeStart()
        {
            Width = 50;
            Height = 50;
            Color = Color.White;
            ShapeBrush = new SolidBrush(Color);
            Id = Guid.NewGuid();
            Name = "开始";
        }

        public override Point? GetNearHandle(Point point)
        {
            var rec = new Rectangle(X, Y, Width, Height);
            if (!rec.Contains(point))
                return null;

            var points = new List<Point> {Hand_1, Hand_2, Hand_3, Hand_4};
            int distance = int.MaxValue;
            int index = -1;
            for (int i = 0; i < points.Count; i++)
            {
                var x = points[i];
                var dist = Math.Abs(x.X - point.X) + Math.Abs(x.Y - point.Y);
                if (dist < distance)
                {
                    distance = dist;
                    index = i;
                }
            }
            return points[index];
        }

        public override void Paint(Graphics graphics)
        {
            graphics.FillEllipse(ShapeBrush, X, Y, Width, Height);
            graphics.DrawEllipse(Pen, X, Y, Width, Height);
            base.Paint(graphics);
        }

        public override bool Contian(Point p)
        {
            var rec = new Rectangle(X, Y, Width, Height);
            return rec.Contains(p);
        }
    }
}